package com.atguigui.leetcode1;

import java.util.ArrayList;
import java.util.List;

/**
 * 1253. 重构 2 行二进制矩阵
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/6/29 8:58
 */
public class P1253ReconstructA2RowBinaryMatrix {
    public static void main(String[] args) {

    }

    class Solution {
        public List<List<Integer>> reconstructMatrix(int upper, int lower, int[] colsum) {
            int n = colsum.length;
            int sum = 0, two = 0;
            for (int i = 0; i < n; i++) {
                if (colsum[i] == 2) {
                    ++two;
                }
                sum += colsum[i];
            }

            // 不满足的两个情况，返回空
            if (sum != upper + lower || Math.min(upper, lower) < two) {
                return new ArrayList<List<Integer>>();
            }

            // 减去所有为2的倍数，剩下的就都是1,0不会再内
            upper -= two;
            lower -= two;

            ArrayList<List<Integer>> res = new ArrayList<>();

            for (int i = 0; i < 2; i++) {
                res.add(new ArrayList<Integer>());
            }

            for (int i = 0; i < n; i++) {
                if (colsum[i] == 2) {
                    res.get(0).add(1);
                    res.get(1).add(1);
                } else if (colsum[i] == 1) {
                    if (upper > 0) {
                        res.get(0).add(1);
                        res.get(1).add(0);
                        --upper;
                    } else {
                        res.get(0).add(0);
                        res.get(1).add(1);
                    }
                } else {
                    res.get(0).add(0);
                    res.get(1).add(0);
                }

            }

            return res;

        }
    }
}
